package com.levelup.cr.web.controller;

import com.levelup.cr.model.Node;
import com.levelup.cr.services.NodeListService;
import com.levelup.cr.services.RepositoryResolver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.io.File;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: denis_zavadsky
 * Date: 9/7/13
 * Time: 12:07 PM
 * To change this template use File | Settings | File Templates.
 */

@Controller
@RequestMapping("/repository")
public class RepositoryController {

    private final static String DEFAULT_PATH = File.separator+"level1";
    private final static Integer DEFAULT_DEPTH = 1;

    @Autowired
    private NodeListService nodeListService;

    public void setNodeListService(NodeListService nodeListService) {
        this.nodeListService = nodeListService;
    }

    @RequestMapping("/get/{path}/{depth}")
    public ModelAndView getProduct(@PathVariable("path") String path, @PathVariable("depth") Integer depth){

        if (depth==null){
            depth = -1;
        }
        Node node = nodeListService.getNodeByPath(path,depth);
        ModelAndView modelAndView = new ModelAndView("repository");
        modelAndView.addObject("node",node);
        return modelAndView;

    }

    @RequestMapping("/get")
    public ModelAndView getProduct(){

        Node node = nodeListService.getNodeByPath(DEFAULT_PATH,DEFAULT_DEPTH);
        ModelAndView modelAndView = new ModelAndView("repository");
        modelAndView.addObject("node",node);
        return modelAndView;

    }
}
